﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using RPGX.Data.Models;

using DotNetX;

namespace RPGX.Data.Repositories
{
    public class PlayerRepository : IPlayerRepository
    {
        #region Public Methods

        public Int64 Connect(Int64 clientAccountId)
        {
            Int64 playerId = 0;

            try
            {
                RPGXServerEntities context = new RPGXServerEntities();

                var player = (from p in context.Players
                              where p.ClientAccountId == clientAccountId
                              select p).SingleOrDefault();

                if (player != null)
                {
                    player.LastLoginDate = DateTime.Now;
                    player.IsActive = true;

                    playerId = player.PlayerId;

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                // TODO implement data layer logging
                Debug.Print(ex.Message);
            }

            return playerId;
        }

        public bool Disconnect(Int64 clientAccountId)
        {
            bool res = false;

            try
            {
                RPGXServerEntities context = new RPGXServerEntities();

                var player = (from p in context.Players
                              where p.ClientAccountId == clientAccountId
                              select p).SingleOrDefault();

                if (player != null)
                {
                    player.LastLogoutDate = DateTime.Now;
                    player.IsActive = false;

                    context.SaveChanges();
                }

                res = true;
            }
            catch (Exception ex)
            {
                // TODO implement data layer logging
                Debug.Print(ex.Message);
            }

            return res;
        }

        public PlayerModel GetPlayer(Int64 clientAccountId)
        {
            PlayerModel pm = new PlayerModel();

            try
            {
                RPGXServerEntities context = new RPGXServerEntities();

                var player = (from p in context.Players
                              where p.ClientAccountId == clientAccountId
                              select p).SingleOrDefault();

                if (player != null)
                {
                    pm.ClientAccountId = player.ClientAccountId;
                    pm.Handle = player.Handle;
                    pm.IsActive = player.IsActive;
                    pm.LastLoginDate = player.LastLoginDate.ToDateTimeOrMinValue();
                    pm.LastLogoutDate = player.LastLogoutDate.ToDateTimeOrMinValue();
                    pm.ModelIndex = player.ModelIndex;
                    pm.PlayerId = player.PlayerId;
                    pm.PositionX = (float)player.PositionX;
                    pm.PositionZ = (float)player.PositionZ;
                }
            }
            catch (Exception ex)
            {
                // TODO implement data layer logging
                Debug.Print(ex.Message);
            }

            return pm;
        }

        #endregion
    }
}
